package Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import DBFunction.GroupManager;
import DBFunction.UserManager;

/**
 * Servlet implementation class ServerAddGroup
 */
public class ServerGroupManager extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServerGroupManager() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("groupAction");
		if (action.compareTo("add")==0){
			String inputGroupName = request.getParameter("inputGroupName");
			GroupManager.insert(inputGroupName);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ServerDisplay");
			   dispatcher.forward( request, response );
		}
		if (action.compareTo("delete")==0){
			String inputGroupName = request.getParameter("deleteGroupName");
			try {
				UserManager.setInactiveInGroup(inputGroupName);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			GroupManager.delete(inputGroupName);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ServerDisplay");
			   dispatcher.forward( request, response );
		}
		if (action.compareTo("changename")==0){
			String inputGroupName = request.getParameter("inputGroupName");
			String oldGroupName = request.getParameter("oldGroupName");
			GroupManager.rename(oldGroupName, inputGroupName);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ServerDisplay");
			   dispatcher.forward( request, response );
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
